package com.lsh.controller;

import com.lsh.constants.MessageConstant;
import com.lsh.entity.Result;
import com.lsh.pojo.OrderSetting;
import com.lsh.service.OrderSettingService;
import com.lsh.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    /**
     * 接受传上来的Excel文件，并解析
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        if(!file.isEmpty()){
            try {
                List<String[]> strings = POIUtils.readExcel(file);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] string : strings) {
                    orderSettingList.add(new OrderSetting(sdf.parse(string[0]),Integer.parseInt(string[1])));
                }
                orderSettingService.add(orderSettingList);
                return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("month") String date){
        List<Map> list = orderSettingService.getOrderSettingByMonth(date);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
    }
    @PostMapping("/editNumberByDate")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
